package com.hw.model.common;

import org.springframework.util.CollectionUtils;

import java.io.Serializable;
import java.util.List;

/**
 * 分页
 * @param <T>
 */
public class PageInfo<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    private int pageNum;
    private int pageSize;
    private int size;
    private long total;
    private int pages;
    private List<T> list;

    public PageInfo() {
    }

    public PageInfo(int pageNum, int pageSize) {
        if (pageNum == 1 && pageSize == 2147483647) {
            pageSize = 0;
        }

        this.pageNum = pageNum;
        this.pageSize = pageSize;
    }

    public void setPage(long total, List<T> list) {
        this.list = list;
        this.size = 0;
        if (!CollectionUtils.isEmpty(list)) {
            this.size = list.size();
        }

        this.setTotal(total);
    }

    public void setTotal(long total) {
        this.total = total;
        if (total == -1L) {
            this.pages = 1;
        } else if (this.pageSize > 0) {
            this.pages = (int)(total / (long)this.pageSize + (long)(total % (long)this.pageSize == 0L ? 0 : 1));
        } else {
            this.pages = 0;
        }

    }

    public int getPageNum() {
        return this.pageNum;
    }

    public void setPageNum(int pageNum) {
        this.pageNum = pageNum;
    }

    public int getPageSize() {
        return this.pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getSize() {
        return this.size;
    }

    public void setSize(int size) {
        this.size = size;
    }

    public long getTotal() {
        return this.total;
    }

    public int getPages() {
        return this.pages;
    }

    public void setPages(int pages) {
        this.pages = pages;
    }

    public List<T> getList() {
        return this.list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }
}
